import * as API from '@/api/dataBoard';
import { ref, reactive, onBeforeMount, watch, } from 'vue';
const getMax = (_data: any) => {
  let total = 0;
  _data?.forEach((item: any) => {
    total += item.value;
  });
  return total;
}
export default () => {
  const monthSortData = reactive({
    water: null,
    gas: null,
    elec: null
  });
  const topFiveData = reactive({
    water: null,
    gas: null,
    elec: null
  });
  const topFiveMax = reactive({
    water: 100,
    gas: 100,
    elec: 100
  });
  const sevenTrendData = reactive({
    water: null,
    gas: null,
    elec: null
  });
  const monthTrendData = reactive({
    water: null,
    gas: null,
    elec: null
  });
  const getTopFiveData = async () => {
    const res = await API.getTopFive();
    if (res) {
      topFiveData.water = res.energyTop5['水'];
      topFiveMax.water = getMax(res.energyTop5['水']);
      topFiveData.gas = res.energyTop5['天燃气'];
      topFiveMax.gas = getMax(res.energyTop5['天燃气']);
      topFiveData.elec = res.energyTop5['电'];
      topFiveMax.elec = getMax(res.energyTop5['电'])
    }
  }
  const getSevenTrendData = async () => {
    const res = await API.getSevenTrend();
    if (res) {
      sevenTrendData.water = res.energyTrend['水'];
      sevenTrendData.gas = res.energyTrend['天燃气'];
      sevenTrendData.elec = res.energyTrend['电'];
    }
  }
  const getMonthTrendData = async () => {
    const res = await API.getMonthTrend();
    if (res) {
      monthTrendData.water = res.energyTrend['水'];
      monthTrendData.gas = res.energyTrend['天燃气'];
      monthTrendData.elec = res.energyTrend['电'];
    }
  }
  const getMonthSortData = async () => {
    const res = await API.getMonthSort();
    if (res) {
      monthSortData.water = res.classifyDosage['水'];
      monthSortData.gas = res.classifyDosage['天燃气'];
      monthSortData.elec = res.classifyDosage['电'];
    }
  }
  onBeforeMount(() => {
    getTopFiveData();
    getSevenTrendData();
    getMonthTrendData();
    getMonthSortData();
  })
  return {
    monthSortData,
    topFiveData,
    sevenTrendData,
    monthTrendData,
    topFiveMax
  }
}